package com.antler.luajavalibrary.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

public class FileUtils {
    private FileUtils() {
        throw new AssertionError();
    }

    public static byte[] readAll(InputStream input) throws Exception {
        ByteArrayOutputStream output = new ByteArrayOutputStream(4096);
        byte[] buffer = new byte[4096];
        int len = 0;
        while ((len = input.read(buffer)) > 0) {
            output.write(buffer, 0, len);
        }
        return output.toByteArray();
    }

    public static String readFileContent(String filePath) {
        FileInputStream is = null;
        StringBuilder sb = null;

        try {
            is = new FileInputStream(filePath);
            sb = new StringBuilder();

            byte[] bytes = readAll(is);
            sb.append(bytes);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return (sb != null ? sb.toString() : null);
    }
}
